package de.bazinga.rotw.menu.item;

import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.texture.TextureCache;

public class MenuItem extends BasicMenuItem {
	private static final long serialVersionUID = 1L;
	
	
	private Text textObj = null;
	
	private String command = "";
	private String text = "";
	private ColorRGBA color = null;
	
	private boolean isImage = false;
	private Quad bgQuad = null;
	
	public MenuItem(String text, String command, ColorRGBA color, float scale,float x, float y) {
		super(command);
		// Text
		this.isImage = false;
		this.color = color;
		this.command = command;
		this.text = text;
		
		textObj = Text.createDefaultTextLabel(command, text);
		textObj.setTextureCombineMode(TextureCombineMode.Replace);
		
		textObj.setTextColor(color);
		
		attachChild(textObj);
		
		setLocalTranslation(x, y, 0);
		setLocalScale(scale);
		
		
		updateGeometricState(0.0f, true);
		updateRenderState();
	}

	public MenuItem(String image, String command, float scale,float x, float y, float width, float height) {
		super(command);
		
		this.isImage = true;
		this.command = command;
		this.text = image;
		
		bgQuad = new Quad("command",
				width, 
				height);
		
		bgQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
	 	
		
		bgQuad.setRenderState(TextureCache.getInstance().getTexture("menu/"+image));
		
		
		BlendState bs = Game.getGame().getDisplay().getRenderer().createBlendState();
		bs.setBlendEnabled(true);
		bs.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
		bs.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
		bs.setTestEnabled(true);
		bs.setTestFunction(BlendState.TestFunction.GreaterThan);
		bgQuad.setRenderState(bs);
		
		bgQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
		bgQuad.updateRenderState();
		
		attachChild(bgQuad);

		
		setLocalTranslation(x, y, 0);
		
		
		updateGeometricState(0.0f, true);
		updateRenderState();
	}
	
	public void setTextColor(ColorRGBA color) {
		if(!isImage) {
			textObj.setTextColor(color);
			textObj.updateRenderState();
			
			updateRenderState();
		}
	}
	
	public void setDefaultColor(ColorRGBA color) {
		this.color = color;
	}
	
	
	
	public void setText(String text) {
		this.text = text;
		if(!isImage) {
			this.detachAllChildren();
			
			textObj = Text.createDefaultTextLabel(command, text);
			textObj.setTextureCombineMode(TextureCombineMode.Replace);
			
			textObj.setTextColor(color);
			
			attachChild(textObj);
			
			updateGeometricState(0.0f, true);
			updateRenderState();
		}else{
			// Update Image
			bgQuad.setRenderState(TextureCache.getInstance().getTexture("menu/"+text));
			bgQuad.updateRenderState();
		}
	}
	
	
	public float getX() {
		return getLocalTranslation().x;
	}
	
	public float getY() {
		return getLocalTranslation().y;
	}
	
	public float getWidth() {
		if(textObj != null) {
			return textObj.getWidth();
		}else if(bgQuad != null) {
			return bgQuad.getWidth();
		}
		return 0;
	}
	
	public float getHeight() {
		if(textObj != null) {
			return textObj.getHeight();
		}else if(bgQuad != null) {
			return bgQuad.getHeight();
		}
		return 0;
	}
	
	public String getText() {
		return text;
	}
	
	public ColorRGBA getTextColor() {
		return this.color;
	}
	
	public boolean isImage() {
		return isImage;
	}
}
